package com.platform.scheduleJob.utils;

import com.platform.car.model.Parking;
import com.platform.car.model.Parkinglot;
import com.platform.car.model.Parkuse;
import com.platform.car.service.ParkingService;
import com.platform.car.service.ParkinglotService;
import com.platform.car.service.ParkingpayService;
import com.platform.car.service.ParkuseService;
import org.apache.log4j.Logger;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@DisallowConcurrentExecution
public class parkuseOrderJobFactory implements Job {

    @Autowired
    private ParkinglotService parkinglotService;

    @Autowired
    private ParkingService parkingService;

    @Autowired
    private ParkuseService parkuseService;

    @Autowired
    private ParkingpayService parkingpayService;

    public final Logger log = Logger.getLogger(this.getClass());
    /**
     * 用户预约超时
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        doet();
        timeOut();
    }



    /**
     * 根据停车场设置的免费时间，
     * 用户预约的时间大于停车场设置的免费时间
     * 2017-11-16  韩榕榕 迁移
     * @throws Exception
     */
    public void doet() {
        System.out.println("--------预约超时未进场--------");
        Parkuse parkuse = new Parkuse();//实例化预约对象
        parkuse.setStatus("1");//状态为已经预约
        List<Parkuse> list=parkuseService.selectOrderList(parkuse); //遍历所有已预约的记录
        if(list.size()>0){
            for (Parkuse pk : list) {
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date one = pk.getMakeTime();//预约时间
                Date two = new Date();//当前时间
                long diff =two.getTime() -one.getTime();
                long min = ((diff / (60 * 1000)) );//分钟数
                Parkinglot ppt= parkinglotService.get(pk.getParkinglotId());
                if(ppt!=null){//停车位存在
                    Parking pg= parkingService.get(ppt.getParkingId());
                    if(pg!=null){//停车场存在
                        Long  fen = Long.valueOf(pg.getCharging());//获取停车场设置的预约时长
                        if(min>=fen){//超时未进场
                            Parkuse p = new Parkuse();
                            p.setStatus("4");//超时未进场
                            Long markeTime=one.getTime();//预约时间
                            Long  charging=60000*fen;
                            Long userTimeLong=markeTime+charging;
                            p.setUseTime(new Date(userTimeLong));
                            p.setId(pk.getId());
                            p.setIntro("超时未进场");
                            parkuseService.update(p);
                        }
                    }
                }
            }
        }
    }


    /**
     * 两个时间之间相差距离多少个小时
     * @return 相差天数
     */
    public static long getDistanceHours(Date date1, Date date2) {
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        long hours=0;
        long diff ;
        if(time1<time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        hours=diff / (1000 * 60 * 60);
        return hours;
    }

    /**
     * 超时进场时间超过三个小时形成支付订单
     */
    public  void timeOut() {
        try {
            System.out.println("--------长时间未取消预约--------");
            List<Parkuse> parkuses = parkuseService.selectByStatus("4");//查询所有超时未进场的预约
            for(Parkuse p:parkuses){
                long distanceHours = getDistanceHours(p.getUseTime(), new Date());
                if(distanceHours>=3){
                    parkingpayService.payMoney(p);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}
}
